package net.dntuan.training.spring.mvc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author tuan.dangnhu
 *
 * <br>$LastChangedRevision: 119 $
 * <br>$LastChangedDate: 2014-09-30 09:27:57 +0000 (Tue, 30 Sep 2014) $
 */
@Controller
@RequestMapping(value = "/api")
public class ExtendedTokenController {

    @Autowired
    private DefaultTokenServices tokenServices;

    @RequestMapping(value = "/revoke")
    @ResponseBody
    public boolean revokeToken(@RequestHeader("Authorization") String authorization) {
        String tokenValue = null;
        if (authorization != null) {
            // split by one space to get token value
            String[] parts = authorization.split(" ");
            if (parts != null && parts.length == 2) {
                tokenValue = parts[1];
                return tokenServices.revokeToken(tokenValue);
            }
        }
        return false;
    }
}
